import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Solution791 {
    public String customSortString(String order, String s) {
        int[] f=new int[26];
        for (int i=0;i<order.length();i++){
            f[order.charAt(i)-'a']=i;
        }
        List<Character> list=new ArrayList<>();
        for (char c : s.toCharArray()) {
            list.add(c);
        }
        list.sort(Comparator.comparingInt(a -> f[a - 'a']));
        StringBuilder stringBuilder=new StringBuilder();
        for (Character character : list) {
            stringBuilder.append(character);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution791().customSortString("cba","abcd"));
    }
}
